package com.dh.product.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
* @ClassName: JsonUtil 
* @Description: TODO
* @author dinghao
* @date 2018年11月19日 下午4:18:45 
*
 */
public class JsonUtil {

	private static ObjectMapper objectMapper = new ObjectMapper();

	/**
	 * 转换为json字符串
	 * 
	 * @param object
	 * @return
	 */
	public static String toJson(Object object) {
		try {
			return objectMapper.writeValueAsString(object);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * json转对象
	* @Title: fromJson
	* @author  dinghao
	* @Description: TODO
	* @return Object
	 */
	public static Object fromJson(String string,TypeReference typeReference) {
		try {
			return objectMapper.readValue(string, typeReference);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
